<?php
/**
 * @author spenceryg
 * @date 2022/12/3
 * @time 3:37 下午
 * @focus love & life
 */


namespace app\auto\model;


use app\auto\lib\ControllerGenerator;
use think\Model;

class Controller extends \think\Model
{
    protected $table = 'auto_controller';

    public function method(): \think\model\relation\HasMany
    {
        return $this->hasMany(Method::class);
    }


    public function app(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(App::class);
    }


    /**
     * onBeforeDelete
     * @param Model $model
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function onBeforeDelete(Model $model)
    {
        Method::destroy(function ($query) use ($model) {
            $query->where('controller_id', $model->getAttr('id'));
        });

        ControllerGenerator::deleteFromController($model);
    }
}
